<?php
/**
 * User: hjd-yao
 * Date: 2019/1/28
 * Time: 17:25
 */

namespace App\Http\Controllers\Cms;


use App\Helpers\SelfResponse;
use App\Http\Controllers\BackendController;
use App\Models\Cms\ArticleCatModel;
use Illuminate\Http\Request;

class ArticleCatController extends BackendController
{
    private static $_articleCatModel;

    public function __construct()
    {
        self::$_articleCatModel = new ArticleCatModel();
    }

    /**
     * @title 文章分类
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $list = self::$_articleCatModel->get()->toArray();
        if($list){
            $list = get_parent_ids($list,'id','parent_id');
            $list = list_to_tree($list,true,'id','parent_id');
        }

        return view('backend::cms.article.cat_list',[
            'page_title'=>'文章分类',
            'list'=>$list
        ]);
    }

    /**
     * @title 添加文章分类
     * @param Request $request
     * @param int $parent_id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function addCat(Request $request,$parent_id = 0)
    {
        if($request->isMethod('post')){
            self::$_articleCatModel->name = $request->post('category_name');
            self::$_articleCatModel->title = $request->post('category_title');
            self::$_articleCatModel->parent_id = $request->post('parent_id',$parent_id);
            self::$_articleCatModel->description = $request->post('description');
            self::$_articleCatModel->is_nav = $request->post('is_nav');
            self::$_articleCatModel->key_words = $request->post('key_words');
            self::$_articleCatModel->sort = $request->post('sort');
            if(self::$_articleCatModel->save()){
                return SelfResponse::success('文章分类添加成功','b_ArticleCat_index');
            }
            return SelfResponse::error('添加失败');
        }else{
            $parent_ids = self::$_articleCatModel->pluck('id')->toArray();
            return view('backend::cms.article.cat_add',[
                'page_title'=>'添加文章分类',
                'parent_id'=>$parent_id,
                'parent_ids'=>arr2str($parent_ids)
            ]);
        }
    }

    /**
     * @title 编辑文章分类
     * @param Request $request
     * @param $id
     * @return SelfResponse|\Illuminate\View\View
     */
    public function editCat(Request $request,$id)
    {
        $info = self::$_articleCatModel::find($id);
        if($request->isMethod('post')){
            if(!$info){
                return SelfResponse::error('修改信息不存在');
            }
            $info->title = $request->post('category_title');
            $info->parent_id = $request->post('parent_id',0);
            $info->description = $request->post('description');
            $info->is_nav = $request->post('is_nav');
            $info->key_words = $request->post('key_words');
            $info->sort = $request->post('sort');
            if($info->save()){
                return SelfResponse::success('修改成功','b_ArticleCat_index');
            }
            return SelfResponse::error('修改失败');
        }else{
            if(!$info){
                return redirect()->route('b_ArticleCat_addCat');
            }
            return view('backend::cms.article.cat_edit',[
                'page_title'=>'编辑'.$info->title,
                'info'=>$info->toArray()
            ]);
        }
    }

    /**
     * @title 删除文章分类
     * @param $id
     * @return SelfResponse
     */
    public function delCat($id)
    {
        $info = self::$_articleCatModel::find($id);
        if(!$info){
            return SelfResponse::error('要操作的记录不存在!');
        }
        if(self::$_articleCatModel->where('id',$id)->delete()){

            return SelfResponse::success($info->title.'已被成功删除','b_ArticleCat_index');
        }
        return SelfResponse::error('删除失败');
    }

    /**
     * @title 获取父级分类
     * @return \Illuminate\Http\JsonResponse
     */
    public function getParentCat()
    {
        $list = self::$_articleCatModel->select(['id','parent_id','title'])->get()->toArray();
        $list = format_selectTree($list,true,'id','parent_id','children');
        array_unshift($list,['id'=>0,'name'=>'顶级分类','open'=>false,'checked'=>true]);
        return response()->json([]+$list);
    }

}